using System;
using System.Data;
using System.IO;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using IGlobalist.Cms.Common;
using IGlobalist.Cms.Bll;
using IGlobalist.Cms.Model;
using IGlobalist.Cms.Presentation;

namespace IGlobalist.Cms
{
    public partial class AddImage : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
             IContentCategory category =null;
            if (!Page.IsPostBack)
            {
                if (Request["id"] != null)
                {
                    category = CategoryController.GetCategory(Convert.ToInt64(Request["id"]));
                    tbName.Text = category.Name;
                    tbDescription.Text = category.Description;
                    ViewState["Photo"] = category;
                }
            }
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (ViewState["Photo"] != null)
            {
                IContentCategory category = (IContentCategory)ViewState["Photo"];
                category.Name = tbName.Text;
                category.Description= tbDescription.Text;
                CategoryController.UpdateCategory(category);
                ltlScript.Text = "<script>window.close();window.opener.location.reload();</script>";
            }
            else
            {
                #region add
               
                IList<ICmsClass> cList = ClassController.GetClassListByClassTypeID((short)CmsObjectType.ContentCategory);
                ICmsClass classMedia = new CmsClass();
                foreach (ICmsClass clazz in cList)
                {
                    for (int i = 0; i < clazz.PropertyList.Count; i++)
                        if ((Convert.ToString(clazz.PropertyList[i].Name) == "Type") && (Convert.ToString(clazz.PropertyList[i].Value) == "Media"))
                        {
                            classMedia = clazz;
                            break;
                        }
                }

                long categoryID = 0;
                if (classMedia.ID > 0)
                {
                    if (CheckData())
                    {
                        lblError.Text = "";
                        IContentCategory category = new Category();
                        if (ViewState["Category"] != null)
                        {
                            category = (Category)ViewState["Category"];
                            category.EditedBy = IGlobalistMembership.GetUser().ID;
                            categoryID = category.ID;
                        }
                        category.ExtPropertyList = classMedia.PropertyList;
                        category.Class = classMedia;
                        category.ClassID = classMedia.ID;
                        category.ClassTypeID = Convert.ToInt16(CmsObjectType.ContentCategory);
                        category.Name = tbName.Text;
                        category.ParentID = Convert.ToInt64(Request["prid"]);
                        category.Order = 0;
                        category.Description = tbDescription.Text;
                        category.Author = IGlobalistMembership.GetUser().ID;


                        if (ViewState["Category"] != null)
                            CategoryController.UpdateCategory(category);
                        else
                            categoryID = CategoryController.InsertCategory(category);
                        //upload file
                        try
                        {
                            string path = Path.Combine(DataRootPhysicalPath, "Photos");
                            HttpPostedFile fPost = fuFile.PostedFile;
                            string fName = fuFile.FileName;
                            string fExtention = Path.GetExtension(fPost.FileName);
                            fPost.SaveAs(Path.Combine(path, categoryID + ".jpg"));
                        }
                        catch { }
                        ltlScript.Text = "<script>window.close();window.opener.location.reload();</script>";
                        
                    }

                }
                #endregion
            }
           
        }

        protected Boolean CheckData()
        {
            if (tbName.Text.Trim()=="")
            {
                lblError.Text="You must enter name!";
                return false;
            }
            if (tbName.Text.Trim()=="")
            {
                lblError.Text="You must choose Photo!";
                return false;
            }
            return true;
        }
    }
}
